﻿/*
  Project:       DataAnalyse
  File:          RegisterHandler.ashx
  Description:   
 *               The global verialbles are all in this file.
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Merge 4 old files into 1 file.
 * Add log function.


Notes:
  1.
  2.
 * for "CreatedBy", we didn't handle it now !~.
 * "delete" funtion didnt implement.
 * update 还没完成。。还有问题。。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using DataAnalyseTool.Model;
using DataAnalyseTool.BusinessTier.Bll;
using DataAnalyseTool.Utilities;
using System.Web.SessionState;

namespace DataAnalyseTool.BusinessTier.Handlers
{
  /// <summary>
  /// Summary description for RegisterHandler
  /// </summary>
  public class RegisterHandler : IHttpHandler, IRequiresSessionState
  {
    protected static readonly log4net.ILog log =
       log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public string q1, q2;
    QQRelation qqrelationModel;
    QQRelationBLL qqBLL = new QQRelationBLL();
    UserBLL userBll = new UserBLL();
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string type = context.Request.Params["action"];
      switch (type)
      {
        case "Add":
          AddRegister(context);
          break;
        case "Delete":
          DeleteRegister(context);
          break;
        case "Update":
          UpdateRegister(context);
          break;
        case "Get":
          GetRegister(context);
          break;
        case "Copy":
          CopyRegister(context);
          break;
        case "CheckDate":
          CheckRegisterDate(context);
          break;
        default:
          break;
      }
    }

    #region Get Register
    public void GetRegister(HttpContext ctx)
    {
      DataTable dt;
      string isSearch = ctx.Request.Params["isSearch"] == null ? "" : ctx.Request.Params["isSearch"].ToString();

      SearchPara sp = new SearchPara();
      sp.haschecked = ctx.Request.Params["hasChecked"] == null ? "" : ctx.Request.Params["hasChecked"].ToString();

      sp.chkqq = ctx.Request.Params["chkqq"] == null ? "" : ctx.Request.Params["chkqq"].ToString();
      sp.keywords = ctx.Request.Params["keywords"] == null ? "" : ctx.Request.Params["keywords"].ToString();
      sp.datefrom = ctx.Request.Params["datefrom"] == null ? "" : ctx.Request.Params["datefrom"].ToString();
      sp.dateto = ctx.Request.Params["dateto"] == null ? "" : ctx.Request.Params["dateto"].ToString();
      sp.pricefrom = ctx.Request.Params["pricefrom"] == null ? "" : ctx.Request.Params["pricefrom"].ToString();
      sp.priceto = ctx.Request.Params["priceto"] == null ? "" : ctx.Request.Params["priceto"].ToString();
      sp.chkqq = ctx.Request.Params["chkqq"] == null ? "" : ctx.Request.Params["chkqq"].ToString();
      sp.chbcaifutong = ctx.Request.Params["chbcaifutong"] == null ? "" : ctx.Request.Params["chbcaifutong"].ToString();
      sp.chbcaifutongname = ctx.Request.Params["chbcaifutongname"] == null ? "" : ctx.Request.Params["chbcaifutongname"].ToString();
      sp.chbzhifubaoname = ctx.Request.Params["chbzhifubaoname"] == null ? "" : ctx.Request.Params["chbzhifubaoname"].ToString();
      sp.chbzhifubaoaccount = ctx.Request.Params["chbzhifubaoaccount"] == null ? "" : ctx.Request.Params["chbzhifubaoaccount"].ToString();
      sp.chbfkzhifubaoname = ctx.Request.Params["chbfkzhifubaoname"] == null ? "" : ctx.Request.Params["chbfkzhifubaoname"].ToString();
      sp.chbfkzhifubao = ctx.Request.Params["chbfkzhifubao"] == null ? "" : ctx.Request.Params["chbfkzhifubao"].ToString();
      sp.limitdays = string.IsNullOrEmpty(ctx.Request.Params["limitdays"]) ? 0 : Convert.ToInt32(ctx.Request.Params["limitdays"].ToString());

      sp.userName = ctx.Request.Params["userName"] == null ? "" : ctx.Request.Params["userName"].ToString();
      sp.shopId = ctx.Request.Params["shopId"] == null ? "" : ctx.Request.Params["shopId"].ToString();

      sp.searchScope = ctx.Request.Params["searchScope"] == null ? "" : ctx.Request.Params["searchScope"].ToString();
      sp.isSourceQuery = ctx.Request.Params["isSourceQuery"] == null ? "" : ctx.Request.Params["isSourceQuery"].ToString();
      sp.isDirectAddRegisterFlag = ctx.Request.Params["isDirectAddRegisterFlag"] == null ? "" : ctx.Request.Params["isDirectAddRegisterFlag"].ToString();


      int ItemTotalCount = 0;
      int pagesize = GetPageSize(ctx);
      int page = GetPageIndex(ctx);
      DataSet ds;


      //如果在直接点击“新增登记” 这个时候是不需要 进行搜索
      //如果后期定和需要改。。那只需要判断sp.isDirectAddRegisterFlag 这个字段就ok了~~。
      //2014.9.30 注释以下内容，主要是为了修复 开始日期和结束日期同一天的缘故。
      if (isSearch != string.Empty)
      {
        dt = RegisterBLL.GetAllRegisterBySearchPara(sp, pagesize, page, out ItemTotalCount);
      }
      else
      {
        ds = RegisterBLL.GetAllRegisterByPagination("ProjectFileds", "Shops", "*", "1=1", "Date", pagesize, page, out ItemTotalCount);
        dt = ds.Tables[0];
      }

      //-----------------------------------2014.07.24   start.
      //if (sp.limitdays > 0)
      //{
      //  dt = RegisterBLL.GetAllRegisterBySearchPara(sp, pagesize, page, out ItemTotalCount);
      //}
      ////-----------------------------------2014.07.24  end

      //***********Handler Shop Name*************
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        if (dt.Rows[i]["ShopColor"].ToString().Trim() != "")
        {
          dt.Rows[i]["ShopName"] = dt.Rows[i]["ShopName"] + "," + dt.Rows[i]["ShopColor"];
        }
      }
      //***********Handler Shop Name*************

      double totalCash = 0.0;
      string cashStr = "";
      if (sp.keywords == "")
      {
        if ((sp.userName != "" && sp.userName != "选择用户") || (sp.shopId != "" && sp.shopId != "100000"))
        {
          DataTable dtMoney = RegisterBLL.GetAllMoney(sp);
          for (int i = 0; i < dtMoney.Rows.Count; i++)
          {
            double temp = 0.0;
            bool isOK = double.TryParse(dtMoney.Rows[i]["Cash"].ToString(), out temp);
            if (isOK)
              totalCash += temp;
          }
          cashStr = "金额：" + totalCash.ToString() + " 元(￥)";
        }
      }

      JsonSerializerSettings setting = new JsonSerializerSettings();
      IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
      iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

      setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
      string result = JsonConvert.SerializeObject(new { total = ItemTotalCount, rows = dt, totalMoney = cashStr }, Formatting.Indented, iso);
      ctx.Response.Write(result);
    }
    public Int32 GetPageSize(HttpContext context)
    {
      try
      {
        return Int32.Parse(context.Request["rows"].ToString());
      }
      catch
      {
        return 10;
      }
    }
    public Int32 GetPageIndex(HttpContext context)
    {
      try
      {
        return context.Request["page"].ToString() == "0" ? 1 : Int32.Parse(context.Request["page"].ToString());
        //return Int32.Parse(context.Request["page"].ToString());
      }
      catch
      {
        return 1;
      }
    }


    #endregion

    #region Add Register

    private void AddRegister(HttpContext ctx)
    {
      ctx.Response.ContentType = "text/plain";
      try
      {
        string qq = ctx.Request.Params["QQ"] == null ? "" : ctx.Request.Params["QQ"].ToString().Trim();
        string wangwang = ctx.Request.Params["Wangwang"] == null ? "" : ctx.Request.Params["Wangwang"].ToString().Trim();
        string zhifubaoaccount = ctx.Request.Params["ZhifubaoAccount"] == null ? "" : ctx.Request.Params["ZhifubaoAccount"].ToString().Trim();
        string zhifubaoname = ctx.Request.Params["ZhifubaoName"] == null ? "" : ctx.Request.Params["ZhifubaoName"].ToString().Trim();
        string rebatezhifubao = ctx.Request.Params["RebateZhifubao"] == null ? "" : ctx.Request.Params["RebateZhifubao"].ToString();
        string rebatezhifubaoname = ctx.Request.Params["RebateZhifubaoName"] == null ? "" : ctx.Request.Params["RebateZhifubaoName"].ToString().Trim();
        string caifutongaccount = ctx.Request.Params["CaifutongAccount"] == null ? "" : ctx.Request.Params["CaifutongAccount"].ToString().Trim();
        string caifutongname = ctx.Request.Params["CaifutongName"] == null ? "" : ctx.Request.Params["CaifutongName"].ToString().Trim();
        string shopid = ctx.Request.Params["ShopID"] == null ? "" : ctx.Request.Params["ShopID"].ToString().Trim();
        string cash = ctx.Request.Params["Cash"] == null ? "" : ctx.Request.Params["Cash"].ToString().Trim();
        string cellphone = ctx.Request.Params["Cellphone"] == null ? "" : ctx.Request.Params["Cellphone"].ToString().Trim();
        string fixphone = ctx.Request.Params["Fixphone"] == null ? "" : ctx.Request.Params["Fixphone"].ToString().Trim();
        string receivename = ctx.Request.Params["ReceiveName"] == null ? "" : ctx.Request.Params["ReceiveName"].ToString().Trim();
        string receiveaddress = ctx.Request.Params["ReceiveAddress"] == null ? "" : ctx.Request.Params["ReceiveAddress"].ToString().Trim();
        string receivecellphone = ctx.Request.Params["ReceiveCellphone"] == null ? "" : ctx.Request.Params["ReceiveCellphone"].ToString().Trim();
        string receivefixphone = ctx.Request.Params["ReceiveFixphone"] == null ? "" : ctx.Request.Params["ReceiveFixphone"].ToString().Trim();
        string date = ctx.Request.Params["Date"] == null ? "" : ctx.Request.Params["Date"].ToString() + " " + DateTime.Now.ToLongTimeString();
        string province = ctx.Request.Params["hdprovince"] == null ? "" : ctx.Request.Params["hdprovince"].ToString().Trim();
        string city = ctx.Request.Params["hdcity"] == null ? "" : ctx.Request.Params["hdcity"].ToString().Trim();
        string district = ctx.Request.Params["hddistrict"] == null ? "" : ctx.Request.Params["hddistrict"].ToString().Trim();

        string beizhu = ctx.Request.Params["beizhu"] == null ? "" : ctx.Request.Params["beizhu"].ToString().Trim();
        string bankaccount = ctx.Request.Params["BankAccount"] == null ? "" : ctx.Request.Params["BankAccount"].ToString().Replace(" ", "").Trim();
        string bankname = ctx.Request.Params["BankName"] == null ? "" : ctx.Request.Params["BankName"].ToString().Trim();

        string banktype = ctx.Request.Params["BankType"] == null ? "" : ctx.Request.Params["BankType"].ToString().Trim();
        string source = ctx.Request.Params["Source"] == null ? "" : ctx.Request.Params["Source"].ToString().Trim();
        string qqrelation = ctx.Request.Params["QQRelation"] == null ? "" : ctx.Request.Params["QQRelation"].ToString().Trim();

        Register r = new Register();
        r.Qq = qq;
        r.Wangwang = wangwang;
        r.ZhifubaoAccount = zhifubaoaccount;
        r.ZhifubaoName = zhifubaoname;
        r.RebateZhifubao = rebatezhifubao;
        r.RebateZhifubaoName = rebatezhifubaoname;
        r.CaifutongAccount = caifutongaccount;
        r.CaifutongName = caifutongname;
        r.ShopId = Convert.ToInt32(shopid);
        r.Cash = cash;
        r.CellPhone = cellphone;
        r.FixPhone = fixphone;
        r.ReceiveName = receivename;
        r.ReceiveCellphone = receivecellphone;
        r.ReceiveFixphone = receivefixphone;
        r.ReceiveAddress = receiveaddress;
        r.Date = date;
        r.Province = province;
        r.City = city;
        r.District = district;
        r.Beizhu = beizhu;
        r.BankAccount = bankaccount;
        r.BankName = bankname;
        r.BankType = banktype;
        r.Source = source;
        r.QQRelation = qqrelation;
        
        r.CreatedBy = ContextWeb.CurrentUser.UserName;
        //r.CreatedBy = "Lannnnnnny";

        RegisterBLL.AddRegister(r);
        string returnValue = "{QQ:'" + r.Qq + "',Wangwang:'" + r.Wangwang + "',ZhifubaoAccount:'" + r.ZhifubaoAccount + "',ZhifubaoName:'" + r.ZhifubaoName + "',RebateZhifubao:'" + r.RebateZhifubao + "',RebateZhifubaoName:'" + r.RebateZhifubaoName + "',CaifutongAccount:'" + r.CaifutongAccount + "',CaifutongName:'" + r.CaifutongName + "',ShopID:'" + r.ShopId + "',Cash:'" + r.Cash + "',Cellphone:'" + r.CellPhone + "',Fixphone:'" + r.FixPhone + "',ReceiveName:'" + r.ReceiveName + "',ReceiveAddress:'" + r.ReceiveAddress + "',ReceiveCellphone:'" + r.ReceiveCellphone + "',ReceiveFixphone:'" + r.ReceiveFixphone + "',Date:'" + r.Date + "',QQRelation:'" + r.QQRelation + "'}";

        if (r.QQRelation != "" && r.QQRelation != null)
        {
          q1 = r.Qq;
          q2 = r.QQRelation;
          AddQQRelation();
        }
        ctx.Response.Write(returnValue);
        return;

      }
      catch (Exception ex)
      {
        ctx.Response.Write("{errorMsg:'新增登记失败'}");
        log.Error("新增登记失败： "+ex.ToString());
      }
    }
    private void AddQQRelation()
    {
      QQRelationBLL qqrelationBLL = new QQRelationBLL();
      qqrelationModel = new QQRelation();
      var isExistQQ = qqrelationBLL.IsSameQQ(q1, q2);
      if (isExistQQ.Rows.Count == 0)
      {
        qqrelationModel.MainQQ = q1;
        qqrelationModel.QQ = q2;
        qqrelationModel.CreateTime = DateTime.Now.ToString();
        qqrelationBLL.AddQQRelation(qqrelationModel);
      }
    }
    //private void 
    #endregion

    #region Copy Register

    private void CopyRegister(HttpContext ctx)
    {
      string wangwang = ctx.Request.Params["uWangwang"].ToString();
      string shopname = ctx.Request.Params["uShopName"].ToString();
      string QQ = ctx.Request.Params["qq"];
      string pid = ctx.Request.Params["pid"];
      try
      {
        int ProId = Convert.ToInt32(ctx.Request.Params["ProId"]);

        if (shopname.Contains(','))
          shopname = shopname.Remove(shopname.Length - 1);

        if (QQ.Contains(','))
          QQ = QQ.Remove(QQ.Length - 1);
        RegisterBLL registerBll = new RegisterBLL();
        List<string> allQQs = RegisterBLL.GetAllRelatesQQByQQ(QQ);

        string basicStr = @"select ProjectFileds.*,Shops.ShopName,Shops.ShopColor from ProjectFileds inner join Shops on ProjectFileds.ShopID = Shops.ShopID where ";
        string qqRelate, lastStr;
        DataTable dataRelateQQ;
        DateTime latestDate;

        qqRelate = ""; lastStr = ""; latestDate = DateTime.Now;
        if (allQQs.Count == 0)
        {
          qqRelate += @" ProjectFileds.QQ = N'" + QQ + "'";
        }
        else
        {
          foreach (string qq in allQQs)
            qqRelate += @" ProjectFileds.QQ = N'" + qq + "' or ";
          qqRelate = qqRelate.Remove(qqRelate.LastIndexOf("or"));
        }

        lastStr = basicStr + qqRelate + " order by Date DESC";
        dataRelateQQ = RegisterBLL.GetRegisterDataBySql(lastStr);
        if (dataRelateQQ.Rows.Count > 0)
        {
          latestDate = Convert.ToDateTime(dataRelateQQ.Rows[0]["Date"]);
        }
        string userName = ctx.Request.Params["u"].ToString();

        RegisterBLL.CopyRegister(Convert.ToInt32(pid), userName);
        ctx.Response.Write("{success:'1'}");

        return;
      }
      catch (Exception ex)
      {
        //errorMsg
        ctx.Response.Write("{exist:'1',errorMsg:'" + ex.Message + "'}");
        log.Error("登记[" + wangwang + "]的时候发生了异常，异常信息是：" + ex.Message);
      }
    }
    public int GetDays(DateTime d1, DateTime d2)
    {

      int d = (d2 - d1).Days;
      return d;
    }
    #endregion

    #region Delete Register
    private void DeleteRegister(HttpContext ctx)
    {
      //string proid = ctx.Request.Params["id"].ToString();
      //if (RegisterBLL.DeleteRegister(proid) == 1)
      //{
      //  ctx.Response.Write("{'success':true}");
      //  return;
      //}
      //ctx.Response.Write("{'success':'false'}");

      string username = ctx.Request.Params["username"];
      string password = ctx.Request.Params["password"];
      string proid = ctx.Request.Params["id"].ToString();
      UserBLL userBll = new UserBLL();
      try
      {
        DataTable users = userBll.GetUserByName(username);
        if (users.Rows.Count > 0)
        {
          string temp = Convert.ToString(users.Rows[0]["Password"]);
          if (temp == password)
          {
            if (RegisterBLL.DeleteRegister(proid) == 1)
            {
              ctx.Response.Write("success");
              return;
            }
          }
          else
          {
            ctx.Response.Write("{fail:'false'}");
          }
        }
      }
      catch (Exception ex)
      {
        ctx.Response.Write("{'exception':未知异常}");
      }
    }
    #endregion

    #region Update Register
    private void UpdateRegister(HttpContext ctx)
    {
      int id = Convert.ToInt32(ctx.Request.Params["id"]);
      string oldQQRelation = ctx.Request.Params["OldQQRelation"];
      string qq = ctx.Request.Params["QQ"] == null ? "" : ctx.Request.Params["QQ"].ToString().Trim();
      string wangwang = ctx.Request.Params["Wangwang"] == null ? "" : ctx.Request.Params["Wangwang"].ToString().Trim();
      string zhifubaoaccount = ctx.Request.Params["ZhifubaoAccount"] == null ? "" : ctx.Request.Params["ZhifubaoAccount"].ToString().Trim();
      string zhifubaoname = ctx.Request.Params["ZhifubaoName"] == null ? "" : ctx.Request.Params["ZhifubaoName"].ToString().Trim();
      string rebatezhifubao = ctx.Request.Params["RebateZhifubao"] == null ? "" : ctx.Request.Params["RebateZhifubao"].ToString().Trim();
      string rebatezhifubaoname = ctx.Request.Params["RebateZhifubaoName"] == null ? "" : ctx.Request.Params["RebateZhifubaoName"].ToString().Trim();
      string caifutongaccount = ctx.Request.Params["CaifutongAccount"] == null ? "" : ctx.Request.Params["CaifutongAccount"].ToString().Trim();
      string caifutongname = ctx.Request.Params["CaifutongName"] == null ? "" : ctx.Request.Params["CaifutongName"].ToString().Trim();
      string shopid = ctx.Request.Params["ShopID"] == null ? "0" : ctx.Request.Params["ShopID"].ToString().Trim();
      string cash = ctx.Request.Params["Cash"] == null ? "" : ctx.Request.Params["Cash"].ToString().Trim();
      string cellphone = ctx.Request.Params["Cellphone"] == null ? "" : ctx.Request.Params["Cellphone"].ToString().Trim();
      string fixphone = ctx.Request.Params["Fixphone"] == null ? "" : ctx.Request.Params["Fixphone"].ToString().Trim();
      string receivename = ctx.Request.Params["ReceiveName"] == null ? "" : ctx.Request.Params["ReceiveName"].ToString().Trim();
      string receiveaddress = ctx.Request.Params["ReceiveAddress"] == null ? "" : ctx.Request.Params["ReceiveAddress"].ToString().Trim();
      string receivecellphone = ctx.Request.Params["ReceiveCellphone"] == null ? "" : ctx.Request.Params["ReceiveCellphone"].ToString().Trim();
      string receivefixphone = ctx.Request.Params["ReceiveFixphone"] == null ? "" : ctx.Request.Params["ReceiveFixphone"].ToString().Trim();
      string date = ctx.Request.Params["Date"] == null ? "" : ctx.Request.Params["Date"].ToString() + " " + DateTime.Now.ToLongTimeString();
      string province = ctx.Request.Params["hdprovince"] == null ? "" : ctx.Request.Params["hdprovince"].ToString();
      string city = ctx.Request.Params["hdcity"] == null ? "" : ctx.Request.Params["hdcity"].ToString();
      string district = ctx.Request.Params["hddistrict"] == null ? "" : ctx.Request.Params["hddistrict"].ToString();
      string beizhu = ctx.Request.Params["beizhu"] == null ? "" : ctx.Request.Params["beizhu"].ToString();
      string bankaccount = ctx.Request.Params["BankAccount"] == null ? "" : ctx.Request.Params["BankAccount"].ToString().Replace(" ", "");
      string bankname = ctx.Request.Params["BankName"] == null ? "" : ctx.Request.Params["BankName"].ToString().Trim();
      string banktype = ctx.Request.Params["BankType"] == null ? "" : ctx.Request.Params["BankType"].ToString().Trim();
      string source = ctx.Request.Params["Source"] == null ? "" : ctx.Request.Params["Source"].ToString();
      string qqrelation = ctx.Request.Params["QQRelation"] == null ? "" : ctx.Request.Params["QQRelation"].ToString().Trim();


      Register r = new Register();
      r.ProId = id;
      r.Qq = qq;
      r.Wangwang = wangwang;
      r.ZhifubaoAccount = zhifubaoaccount;
      r.ZhifubaoName = zhifubaoname;
      r.RebateZhifubao = rebatezhifubao;
      r.RebateZhifubaoName = rebatezhifubaoname;
      r.CaifutongAccount = caifutongaccount;
      r.CaifutongName = caifutongname;
      r.ShopId = Convert.ToInt32(shopid);
      r.Cash = cash;
      r.CellPhone = cellphone;
      r.FixPhone = fixphone;
      r.ReceiveName = receivename;
      r.ReceiveCellphone = receivecellphone;
      r.ReceiveFixphone = receivefixphone;
      r.ReceiveAddress = receiveaddress;
      r.Date = date;
      r.Province = province;
      r.City = city;
      r.District = district;
      r.Beizhu = beizhu;
      r.BankAccount = bankaccount;
      r.BankName = bankname;
      r.BankType = banktype;
      r.Source = source;
      r.QQRelation = qqrelation;

      RegisterBLL.UpdateRegister(r, "ProID", r.ProId.ToString());
      string returnValue = "{QQ:'" + r.Qq + "',Wangwang:'" + r.Wangwang + "',ZhifubaoAccount:'" + r.ZhifubaoAccount + "',ZhifubaoName:'" + r.ZhifubaoName + "',RebateZhifubao:'" + r.RebateZhifubao + "',RebateZhifubaoName:'" + r.RebateZhifubaoName + "',CaifutongAccount:'" + r.CaifutongAccount + "',CaifutongName:'" + r.CaifutongName + "',ShopID:'" + r.ShopId + "',Cash:'" + r.Cash + "',Cellphone:'" + r.CellPhone + "',Fixphone:'" + r.FixPhone + "',ReceiveName:'" + r.ReceiveName + "',ReceiveAddress:'" + r.ReceiveAddress + "',ReceiveCellphone:'" + r.ReceiveCellphone + "',ReceiveFixphone:'" + r.ReceiveFixphone + "',Date:'" + r.Date + "',Source:'" + r.Source + "',QQRelation:'" + r.QQRelation + "'}";

      QQRelationBLL qqrelationBLL = new QQRelationBLL();
      qqrelationModel = new QQRelation();
      if (r.QQRelation != "" && r.QQRelation != null)
      {
        try
        {
          if (oldQQRelation != r.QQRelation)
          {
            q1 = r.Qq;
            q2 = r.QQRelation;

            var dtResult = qqrelationBLL.IsSameQQ(r.Qq, oldQQRelation);
            qqrelationModel.MainQQ = q1;
            qqrelationModel.QQ = q2;
            qqrelationModel.CreateTime = DateTime.Now.ToString();
            if (dtResult.Rows.Count > 0)
            {
              qqrelationModel.QQMappID = Convert.ToInt32(dtResult.Rows[0]["QQMappID"]);
              qqrelationBLL.UpdateQQRelation(qqrelationModel);
              log.Info("更新QQ关联完成。");
            }
            else
            {
              qqrelationBLL.AddQQRelation(qqrelationModel);
              log.Info("插入一条新的QQ关联信息");
            }
          }
        }
        catch (Exception ex)
        {
          log.Error("发现异常信息：" + ex.Message);
        }
      }
      else
      {
        try
        {
          var dtResult = qqrelationBLL.IsSameQQ(r.Qq, oldQQRelation);
          if (dtResult.Rows.Count > 0)
          {
            string qqID = Convert.ToString(dtResult.Rows[0]["QQMappID"]);
            qqrelationBLL.DeleteQQRelation(qqID);
          }
        }
        catch (Exception ex)
        {
          log.Info("删除QQ关联信息的时候出现错误: " + ex.Message);
        }
      }
      ctx.Response.Write(returnValue);
    }
    #endregion

    #region Check Date
    private void CheckRegisterDate(HttpContext ctx)
    {
      string qq, shopidStr;
      int shopID;

      shopID = 0;
      qq = ctx.Request.QueryString["qq"].ToString();
      shopidStr = ctx.Request.QueryString["shop"].ToString();
      ShopBLL shop = new ShopBLL();
      try
      {
        if (shopidStr != "")
        {
          if (!Utility.CheckAllIsNumbers(shopidStr))
          {
            if (shopidStr.Contains(','))
            {
              shopidStr = shopidStr.Remove(shopidStr.LastIndexOf(','));
              shopID = shop.getShopId(shopidStr);
            }
            else
            {
              shopID = shop.getShopId(shopidStr);
            }
          }
          else
            shopID = Convert.ToInt32(shopidStr);

          //看看最近15天是否有录入数据~~
          if (RegisterBLL.GetRegister(qq, shopID, 15).Rows.Count < 1)
          {
            ctx.Response.Write("false");
          }
          else
          {
            ctx.Response.Write("true");
          }
        }
        else
        {
          log.Error("发现 传入的 shopidStr 字段值为空值！");
          ctx.Response.Write("error");
        }
      }
      catch (Exception ex)
      {
        log.Error("发现异常：" + ex.Message + "输入的QQ：" + qq + " 和shopid： " + shopidStr);
      }
    }
    #endregion

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}